/*
 * Copyright (c) 2017-present, CV4J Contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.core.filters;

import com.cv4j.core.datamodel.ImageProcessor;
import com.cv4j.image.util.Tools;

import java.util.Optional;

/**
 * 盒子模糊滤波器
 *
 * @author dev
 * @date 2021/07/22
 */
public class BoxBlurFilter extends BaseFilter {
    private int hRadius = 5;
    private int vRadius = 5;
    private int iterations = 1;

    /**
     * 做的过滤器
     *
     * @param src src
     * @return {@link ImageProcessor}
     */
    @Override
    public Optional<ImageProcessor> doFilter(ImageProcessor src) {
        byte[][] output = new byte[3][width * height];
        byte[][] input = new byte[][]{RED, GREED, BLUE};
        for (int i = 0; i < iterations; i++) {
            blur(input, output, width, height, hRadius);
            blur(output, input, height, width, vRadius);
        }
        return Optional.ofNullable(src);
    }

    /**
     * 模糊
     *
     * @param in 在
     * @param out 出
     * @param width 宽度
     * @param height 高度
     * @param radius 半径
     */
    private void blur(byte[][] in, byte[][] out, int width, int height, int radius) {
        int widthMinus1 = width - 1;
        int tableSize = 2 * radius + 1;
        int[] divide = new int[256 * tableSize];

        // the value scope will be 0 to 255, and number of 0 is table size
        // will get means from index not calculate result again since 
        // color value must be  between 0 and 255.
        for (int i = 0; i < 256 * tableSize; i++) {
            divide[i] = i / tableSize;
        }
        int inIndex = 0;
        // 每一行
        for (int y = 0; y < height; y++) {
            int outIndex = y;
            int tr = 0;
            int tg = 0;
            int tb = 0;
            // 初始化盒子里面的像素和
            for (int i = -radius; i <= radius; i++) {
                int offset = inIndex + Tools.clamp(i, 0, width - 1);
                tr += in[0][offset] & 0xff;
                tg += in[1][offset] & 0xff;
                tb += in[2][offset] & 0xff;
            }
            // 每一列，每一个像素
            for (int x = 0; x < width; x++) {
                // 赋值到输出像素
                out[0][outIndex] = (byte) divide[tr];
                out[1][outIndex] = (byte) divide[tg];
                out[2][outIndex] = (byte) divide[tb];
                // 移动盒子一个像素距离
                int i1 = x + radius + 1;
                // 检测是否达到边缘
                if (i1 > widthMinus1) {
                    i1 = widthMinus1;
                }
                // 将要移出的一个像素
                int i2 = x - radius;
                if (i2 < 0) {
                    i2 = 0;
                }
                // 计算移除与移进像素之间的差值，更新像素和
                tr += (in[0][inIndex + i1] & 0xff) - (in[0][inIndex + i2] & 0xff);
                tg += (in[1][inIndex + i1] & 0xff) - (in[1][inIndex + i2] & 0xff);
                tb += (in[2][inIndex + i1] & 0xff) - (in[2][inIndex + i2] & 0xff);
                // 继续到下一行
                outIndex += height;
            }
            // 继续到下一行
            inIndex += width;
        }
    }

    /**
     * 设置hradius
     *
     * @param hRadius h半径
     */
    public void setHRadius(int hRadius) {
        this.hRadius = hRadius;
    }

    /**
     * 得到hradius
     *
     * @return int
     */
    public int getHRadius() {
        return hRadius;
    }

    /**
     * 设置vradius
     *
     * @param vRadius v半径
     */
    public void setVRadius(int vRadius) {
        this.vRadius = vRadius;
    }

    /**
     * 得到vradius
     *
     * @return int
     */
    public int getVRadius() {
        return vRadius;
    }

    /**
     * 设置半径
     *
     * @param radius 半径
     */
    public void setRadius(int radius) {
        this.hRadius = this.vRadius = radius;
    }

    /**
     * 得到半径
     *
     * @return int
     */
    public int getRadius() {
        return hRadius;
    }

    /**
     * 设置迭代
     *
     * @param iterations 迭代
     */
    public void setIterations(int iterations) {
        this.iterations = iterations;
    }

    /**
     * 得到的迭代
     *
     * @return int
     */
    public int getIterations() {
        return iterations;
    }
}
